package com.inspur.gs.sbssdl.sbwhgl.gyjgl.gyjgl.front.ghlydxz_frm.voactions;

import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.action.query.BeforeQueryAction;
import com.inspur.edp.cef.entity.condition.*;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ghlyxzSearchBeforeVOAction extends BeforeQueryAction {

    public ghlyxzSearchBeforeVOAction(QueryContext context) {
        super(context);
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            String ghid = "4939bfe5-a636-432e-bc91-65de73b8b1ee";
            IStandardLcp ghlcp = iLcpFactory.createLcpByBEId(ghid);

            List<IEntityData> ghDatas = ghlcp.query();
            if (!ghDatas.isEmpty()){
                List<String> lybhids = ghDatas.stream()
                        .map(s -> s.getValue("lybh").toString())
                        .distinct()
                        .collect(Collectors.toList());
                EntityFilter filter = getQueryContext().getFilter();
                ArrayList<FilterCondition> filterConditions = (ArrayList<FilterCondition>) FilterCondition.builder().andNotIn("sqdh", lybhids).build();
                filter.addFilterConditions(filterConditions);
            }
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }
}
